package com.joysuch.wwyt.wwyt_framework;

import org.apache.commons.lang3.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * Author:  feizhou
 * Date:    2022/8/18 19:59
 */
public class LocalDateUtil {


	public static LocalDate transferString2LocalDate(String s) {
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		return LocalDate.parse(s, fmt);
	}

	public static Long claculateGapBetweenTwoLocalDate(LocalDate localDateStart, LocalDate localDateEnd) {
		Duration duration = Duration.between(localDateStart.atStartOfDay(), localDateEnd.atStartOfDay());
		return duration.toDays();
	}

	public static long parseDate(String date) {
		if (StringUtils.isEmpty(date)) {
			return 0L;
		}

		if (!date.contains("-")) {
			if (date.length() == 4) {
				DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy");
				LocalDate of = LocalDate.of(Integer.parseInt(date), 1, 1);
				long l = convertLocalDate2TimeStamp(of);
				return l;
			}
			if (date.length() == 5) {
				LocalDateTime time = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
				String[] times = date.split(":");
				long hour = Long.parseLong(times[0]);
				long min = Long.parseLong(times[1]);
				time = time.plus(hour, ChronoUnit.HOURS);
				time = time.plus(min, ChronoUnit.MINUTES);
				long l = convertLocalDateTime2TimeStamp(time);
				return l;
			} else if (date.length() == 8) {
				LocalDateTime time = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
				String[] times = date.split(":");
				long hour = Long.parseLong(times[0]);
				long min = Long.parseLong(times[1]);
				long seconds = Long.parseLong(times[2]);
				time = time.plus(hour, ChronoUnit.HOURS);
				time = time.plus(min, ChronoUnit.MINUTES);
				time = time.plus(seconds, ChronoUnit.SECONDS);
				long l = convertLocalDateTime2TimeStamp(time);
				return l;
			}
		} else {
			if (date.length() == 7) {
				DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM");
				String[] items = date.split("-");
				Integer year = Integer.parseInt(items[0]);
				Integer month = Integer.parseInt(items[1]);

				LocalDate of = LocalDate.of(year, month, 1);
				long l = convertLocalDate2TimeStamp(of);
				return l;
			}
			if (date.length() == 10) {
				DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
				long l = convertLocalDate2TimeStamp(LocalDate.parse(date, df));
				return l;
			}
			if (date.length() == 16) {
				DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
				long l = convertLocalDateTime2TimeStamp(LocalDateTime.parse(date, df));
				return l;
			}
			if (date.length() == 19) {
				DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
				long l = convertLocalDateTime2TimeStamp(LocalDateTime.parse(date, df));
				return l;
			}
		}

		return 0L;
	}

	public static void main(String[] args) {
		long l = parseDate("2014-02-03 01:10:31");
		System.out.println(l);
	}

	public static long convertLocalDate2TimeStamp(LocalDate localDate) {
		return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
	}

	public static long convertLocalDateTime2TimeStamp(LocalDateTime localDate) {
		return localDate.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
	}
}
